# Direct outputs eligible for public release

# GLOBAL SETTINGS --------------------------------------------------------------

options(
    scipen = 999,
    digits = 16,
    max.print = .Machine$integer.max,
    show.error.locations = TRUE,
    warn = 1
)

RNGkind("L'Ecuyer-CMRG")
seed <- 818675309L
set.seed(seed) # setting main seed

# PACKAGES ---------------------------------------------------------------------
library(data.table)
library(ggplot2)
library(scales)

# PACKAGE SETTINGS -------------------------------------------------------------

# data.table
setDTthreads(threads = 1L)
options(datatable.print.class = TRUE, datatable.print.keys = TRUE)
# so that printing the data.table also shows the variable type on top

# BEGIN FILE -------------------------------------------------------------------

# Read in all estimates

any_job_to_job <-
    readRDS(
        "~/estimation-output/unearned_income_effect_estimates_firm_move.rds"
    )
any_job_to_job <-
    any_job_to_job[
        model == "reduced_form" & ref_event_time %in% c(-7:5)
    ]
any_job_to_job[, outcome_label := "Any Job-to-Job Move"]

down_job_to_job <-
    readRDS(
        "~/estimation-output/unearned_income_effect_estimates_firm_move_down.rds"
    )
down_job_to_job <-
    down_job_to_job[
        model == "reduced_form" & ref_event_time %in% c(-7:5)
    ]
down_job_to_job[, outcome_label := "Downward Move"]

up_job_to_job <-
    readRDS(
        "~/estimation-output/unearned_income_effect_estimates_firm_move_up.rds"
    )
up_job_to_job <-
    up_job_to_job[
        model == "reduced_form" & ref_event_time %in% c(-7:5)
    ]
up_job_to_job[, outcome_label := "Upward Move"]

job_to_job <-
    rbindlist(
        list(any_job_to_job, down_job_to_job, up_job_to_job),
        use.names = TRUE
    )

# Add omitted; recall for these outcomes it was -1
temp <-
    CJ(
        outcome_label = unique(job_to_job$outcome_label),
        model = "reduced_form",
        income_quartile = 5
    )
temp[, ref_event_time := -1]
temp[, estimate := 0]
temp[, cluster_se := 0]

job_to_job <- rbindlist(list(job_to_job, temp), use.names = TRUE)
temp <- NULL
rm(temp)

job_to_job[
    ,
    outcome_label :=
        factor(
            outcome_label,
            levels = c("Any Job-to-Job Move", "Downward Move", "Upward Move")
        )
]

# Produce Figure B.11d
# Rate and direction of job-to-job moves (percentage points)
job_to_job[, estimate := estimate * 100]
job_to_job[, cluster_se := cluster_se * 100]

figure_b_11_d <-
    ggplot(
        aes(
            x = ref_event_time,
            y = estimate,
            color = factor(outcome_label),
            fill = factor(outcome_label)
        ),
        data = job_to_job
    ) +
    geom_line() +
    geom_ribbon(
        aes(
            ymin = estimate - (1.64 * cluster_se),
            ymax = estimate + (1.64 * cluster_se)
        ),
        linetype = 0,
        alpha = 0.2,
        show.legend = FALSE
    ) +
    theme_bw(base_size = 13) +
    theme(panel.grid.minor = element_blank()) +
    scale_x_continuous(
        breaks = seq.int(from = -7L, to = 5L, by = 1L),
        expand = expansion(mult = c(0.0025, 0.0025))
    ) +
    scale_y_continuous(breaks = seq.int(from = -20, to = 25, by = 5) / 10) +
    labs(x = "Event Time (ℓ)", y = "Event Study Estimate (pp)") +
    theme(
        legend.title = element_blank(),
        legend.position = c(0.1625, 0.8825),
        legend.background = element_rect(fill = "white", color = "grey"),
        strip.text.x = element_blank(),
        strip.background = element_blank(),
        legend.key = element_rect(NA),
        legend.spacing.y = unit(2, "mm"),
        legend.key.height = unit(4, "mm"),
        legend.key.width = unit(4, "mm"),
        legend.margin = margin(t = 0, r = 0.1, b = 0.15, l = 0.1, unit = "cm")
    ) +
    scale_color_viridis_d() +
    scale_fill_viridis_d() +
    guides(color = guide_legend(ncol = 1, byrow = TRUE)) +
    coord_cartesian(ylim = c(-2, 2.5))

ggsave(
    plot = figure_b_11_d,
    filename = "~/paper/figures/figure-B.11d.png",
    width = 6,
    height = 4,
    dpi = 600
)

ggsave(
    plot = figure_b_11_d,
    filename = "~/paper/figures/figure-B.11d.tif",
    width = 6,
    height = 4,
    device = "tiff",
    dpi = 600
)

# Housekeeping
any_job_to_job <- NULL
down_job_to_job <- NULL
up_job_to_job <- NULL
job_to_job <- NULL
figure_b_11_d <- NULL
rm(any_job_to_job, down_job_to_job, up_job_to_job, job_to_job, figure_b_11_d)